//
//  DeviceVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/8.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "DeviceVC.h"
#import "DeviceCell.h"
#import "BaseCell.h"
#import "DeviceModel.h"

@interface DeviceVC ()<UITableViewDataSource,UITableViewDelegate,deviceDelegate>
@property (strong, nonatomic) IBOutlet GlobalTabel *tab;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topHeight;

@end

@implementation DeviceVC{
    RequestServices *service;
    NSArray *list_arr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设备管理";
    [self.backKuang setHidden:YES];
    [self showBackCenterKuang:[UIImage imageNamed:@"back_center1"]];
    self.tab.delegate = self;
    self.tab.dataSource = self;
    [self.tab registerNib:[UINib nibWithNibName:@"DeviceCell" bundle:nil] forCellReuseIdentifier:@"DeviceCell"];
    self.tab.tableFooterView = [UIView new];
    
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    [service getDeviceList:8000];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    list_arr = [DeviceModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
    [self.tab reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self ShowNewMsg:error.domain];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return list_arr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Is_iPhone ? 90 : 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DeviceCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    cell.model = list_arr[indexPath.row];
    cell.deleate_btn.tag = indexPath.row;
    if (indexPath.row == 0) {
        cell.current_lab.text = @"(当前设备)";
    }else{
        cell.current_lab.text = @"";
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return SCREEN_H * 0.1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (list_arr.count > 0) {
        BaseCell *view = [[[NSBundle mainBundle]loadNibNamed:@"BaseCell" owner:self options:nil]lastObject];
        view.left_lab.text = @"文件说明";
        view.left_lab.font = AutoLabFont2;
        view.backgroundColor = [UIColor clearColor];
        return view;
    }else{
        return nil;
    }
}

//移除设备
-(void)delegateDevice:(UIButton *)sender{
    DeviceModel *model = list_arr[sender.tag];
    [service removeDevice:model.deviceId tag:8001];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
